package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.PromotionAd;
import com.lagou.domain.PromotionAdVo;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {

    @Autowired
    private PromotionAdService promotionAdService;

    @RequestMapping("/findAllPromotionAdByPage")
//    public ResponseResult findAllAdByPage(@RequestBody PromotionAdVo promotionAdVo) {
    public ResponseResult findAllAdByPage(PromotionAdVo promotionAdVo) { // 这是get查询所用

        PageInfo<PromotionAd> promotions = promotionAdService.findAllPromotionAdByPage(promotionAdVo);

        ResponseResult result = new ResponseResult(true, 200, "广告分页查询成功", promotions);

        return result;
    }

    @RequestMapping("/PromotionAdUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

        if (file.isEmpty()) {
            throw new RuntimeException();
        }

        String realPath = request.getServletContext().getRealPath("/");

        String substring = realPath.substring(0, realPath.indexOf("ssm_web"));

        String originalFilename = file.getOriginalFilename();

        String newFileName = System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));

        String uploadPath = substring + "upload\\";

        File filePath = new File(uploadPath, newFileName);

        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
            System.out.println("创建目录成功");
        }

        file.transferTo(filePath);

        Map<String, String> map = new HashMap<>();
        map.put("fileName", newFileName);
        map.put("filePath", "http://localhost:8080/upload/" + newFileName);

        ResponseResult responseResult = new ResponseResult(true, 200, "图片上传成功", map);

        return responseResult;
    }

    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(Integer id, Integer status) {

        promotionAdService.updatePromotionAdStatus(id, status);

        ResponseResult result = new ResponseResult(true, 200, "广告上下线成功", null);

        return result;
    }

    @RequestMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd) {

        ResponseResult result = null;
        if (promotionAd.getId() == null) {
            promotionAdService.savePromotionAd(promotionAd);
            result = new ResponseResult(true, 200, "新增广告信息成功", null);
        } else {
            promotionAdService.updatePromotionAd(promotionAd);
            result = new ResponseResult(true, 200, "修改广告信息成功", null);
        }

        return result;
    }

    @RequestMapping("/findPromotionAdById")
    public ResponseResult findPromotionAdById(Integer id) {

        PromotionAd promotionAd = promotionAdService.findPromotionAdById(id);

        ResponseResult result = new ResponseResult(true, 200, "响应成功", promotionAd);

        return result;
    }
}
